package com.example.myapplication

import android.media.Image
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.myapplication.ui.theme.MyApplicationTheme
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.height
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.navigation.compose.*
import android.media.MediaPlayer
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.widget.Toast
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Slider
import androidx.compose.material3.SliderState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.core.net.toUri
import androidx.core.content.edit
import java.io.File

//private val MainActivity.innerPadding: Any

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        val pref = getSharedPreferences("LibPath", Context.MODE_PRIVATE)
        StaticStorage.Lib = File(pref.getString("LibPath", ""))
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent()
        {
            MyApplicationTheme()
            {
                val navController = rememberNavController()
                val currentRoute = navController.currentBackStackEntry?.destination?.route

                NavHost(navController, startDestination = "login")
                {
                    composable("login"){ Login { navController.navigate("main") } }
                    composable("main"){ MainAppShell(navController) { MainPage() } }
                    composable("config"){ MainAppShell(navController) { Config() } }
                }
            }
        }
    }
}


@Composable
fun Login(modifier: Modifier = Modifier, login: ()-> Unit) {
    var account by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    val context = LocalContext.current;
    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "TestApp", fontSize = 50.sp, modifier = Modifier.padding(bottom = 32.dp))

        OutlinedTextField(
            value = account,
            onValueChange = { account = it },
            label = { Text("账号") },
            modifier = Modifier.fillMaxWidth(0.8f)
        )

        Spacer(modifier = Modifier.height(16.dp))

        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            label = { Text("密码") },
            visualTransformation = PasswordVisualTransformation(),
            modifier = Modifier.fillMaxWidth(0.8f)
        )
        Spacer(modifier = Modifier.height(60.dp))
        Row {
            Button({ AccountManager.login(context, account, password.hashCode(), login) }, modifier= Modifier.width(150.dp)){Text("登录")}
            Spacer(modifier = Modifier.width(20.dp))
            Button({ AccountManager.registerAccount(context, account, password.hashCode()) }, modifier= Modifier.width(150.dp)){Text("注册")}
        }
    }
    LaunchedEffect(Unit) { AccountManager.autoLogin(context, login); loadFileList(context, "cache/Favourite.dat", StaticStorage.favouriteList); scanFolder(context,
        StaticStorage.Lib)}
}